/********************************************************************************
 *  
 *  Product: GSAOBKeyMatchesLib
 *  Description: A C# API for Responsing to Google Search Appliance OneBoxModule.
 *
 *  (c) Copyright 2007 
 *  
********************************************************************************/
using System;
using System.Collections;
using System.Text;

namespace GSAOBKeyMatchesLib.Server
{
    /// <summary>
    /// Provides provider wich Process Request,checks request parameters and build Response
    /// <para>Author Albert Ghukasyan</para>
    /// </summary>
    public class OneBoxProvider
    {
        #region Variables

        protected String apiMaj = "";
        protected String apiMin = "";
        protected String oneboxName = "";
        protected String dateTime = "";
        protected String ipAddr = "";
        protected String lang = "";
        protected String query = "";
        protected String authType = "";
        protected String username = "";
        protected String password = "";
        protected String distinguishedName = "";
        protected String cookieName = "";
        protected ArrayList _matchGroups = null;
        protected String URL_PREFIX = "";
        protected String KeymatchFile = "";

        #endregion

        #region getURL

        protected virtual String getURL()
        {
            return URL_PREFIX+"?"+ "apiMaj=" + this.apiMaj + "&apiMin=" + this.apiMin + "&oneboxName=" + this.oneboxName +
              "&dateTime=" + this.dateTime + "&ipAddr=" + this.ipAddr + "&lang=" + this.lang + "&query=" + this.query +
              "&authType="+this.authType;
        }

        #endregion

        #region ProcessRequest (Handled In Derived Class)

        /// <summary>
        /// Provides Methode wich exams wich of 4 provideOneBoxResults need to call
        /// </summary>
        /// <param name="request"></param>
        /// <param name="URL_PREFIX"></param>
        /// <param name="KeymatchFile"></param>
        public virtual void processRequest(System.Web.HttpRequest request,string URL_PREFIX,string KeymatchFile)
        {

            this.KeymatchFile = KeymatchFile;
            this.URL_PREFIX = URL_PREFIX;
            this.apiMaj = request.QueryString["apiMaj"]; // integer (1,2,3,...)
            this.apiMin = request.QueryString["apiMin"]; // integer (0,1,2,3,...)
            this.oneboxName = request.QueryString["oneboxName"];
            this.dateTime = request.QueryString["dateTime"] ?? ""; // optional, UTC format
            this.ipAddr = request.QueryString["ipAddr"] ?? ""; // optional
            this.lang = request.QueryString["lang"]; // two-character language code
            this.query = request.QueryString["query"]; // UTF-8 encoded, URL-escaped

            // optional, match groups from regular expression evaluation
            int count = 0;
            this._matchGroups = new ArrayList();
            String match = request.QueryString["p" + (count++)];
            while (match != null)
            {
                this._matchGroups.Add(match);
                match = request.QueryString["p" + (count++)];
            }

            IOneBoxResults res = null;
            try
            {
                // authType == none | basic | ldap | sso
                this.authType = request.QueryString["authType"];
                if ("none".Equals(authType))
                {
                    res = provideOneBoxResults(this.apiMaj, this.apiMin, this.oneboxName, this.dateTime,
                            this.ipAddr, this.lang, this.query, this._matchGroups);
                }
                else if ("basic".Equals(authType))
                {
                    this.username = request.QueryString["userName"];
                    this.password = request.QueryString["password"];
                    res = provideOneBoxResults(this.apiMaj, this.apiMin, this.oneboxName, this.dateTime,
                            this.ipAddr, this.username, this.password, this.lang, this.query, this._matchGroups);
                }
                else if ("ldap".Equals(authType))
                {
                    // LDAP distinguished name (DN)
                    this.distinguishedName = request.QueryString["userName"];
                    res = provideOneBoxResults(this.apiMaj, this.apiMin, this.oneboxName, this.dateTime,
                            this.ipAddr, this.distinguishedName, this.lang, this.query, this._matchGroups);
                }
                else if ("sso".Equals(authType))
                {
                    System.Web.HttpCookie ssoCookie = null;
                    this.cookieName = request.QueryString["userName"];
                    System.Web.HttpCookieCollection cookies = request.Cookies;
                    if (cookies != null)
                    {
                        for (int i = 0; i < cookies.Count; i++)
                        {
                            System.Web.HttpCookie c = cookies[i];
                            if (c.Name.Equals(this.cookieName))
                            {
                                ssoCookie = c;
                                break;
                            }
                        }
                    }
                    res = provideOneBoxResults(this.apiMaj, this.apiMin, this.oneboxName, this.dateTime,
                            this.ipAddr, ssoCookie, this.lang, this.query, this._matchGroups);
                }
                else
                {
                    res = new OneBoxResults();
                    ((OneBoxResults)res).setFailure(FailureCodes.securityFailure,
                            "User authentication type not recognized: " + authType);
                }
            }
            catch (Exception osoe)
            {
                res = new OneBoxResults();
                ((OneBoxResults)res).setFailure(FailureCodes.securityFailure,
                        osoe.Message);
            }

        }

        #endregion

        #region ProvideOneBoxResults virtual functions(Handled in Derived Class)

        protected virtual IOneBoxResults provideOneBoxResults(String apiMaj, String apiMin, String oneboxName,
                String dateTime, String ipAddr, String lang, String query, ArrayList matchGroups)
        {
            return null;
        }


        protected virtual IOneBoxResults provideOneBoxResults(String apiMaj, String apiMin, String oneboxName,
                String dateTime, String ipAddr, String userName, String password, String lang,
                String query, ArrayList matchGroups)
        {
            return null;
        }

        protected virtual IOneBoxResults provideOneBoxResults(String apiMaj, String apiMin, String oneboxName,
                String dateTime, String ipAddr, String distinguishedName, String lang, String query,
                ArrayList matchGroups)
        {
            return null;
        }

        protected virtual IOneBoxResults provideOneBoxResults(String apiMaj, String apiMin, String oneboxName,
                String dateTime, String ipAddr, System.Web.HttpCookie ssoCookie, String lang, String query,
                ArrayList matchGroups)
        {
            return null;
        }

        #endregion
    }
}
